package pack05_covid_sort;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @author lilulu
 * @date 2023/2/11 13:44
 */
public class CovidSortBean implements WritableComparable<CovidSortBean> {

    private String state;
    private int cases;
    private int deaths;

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public int getCases() {
        return cases;
    }

    public void setCases(int cases) {
        this.cases = cases;
    }

    public int getDeaths() {
        return deaths;
    }

    public void setDeaths(int deaths) {
        this.deaths = deaths;
    }

    public CovidSortBean() {
    }

    public CovidSortBean(String state, int cases, int deaths) {
        this.state = state;
        this.cases = cases;
        this.deaths = deaths;
    }

    /*
      1:在MR中对K2的排序不用我们去排序，我们只需要指定排序规则，MR会自动按照我们设置的规则进行排序
         1) 我们按照确诊病例进行降序排序
         2) compareTo只需要告诉MR什么时候返回大于0的值，什么时候返回小于0的值，什么时候返回等于0的值
      2:MR内部使用的是：归并和快排
     */
    @Override
    public String toString() {
        return state +"\t" + cases+"\t"+ deaths;
    }


    @Override
    public int compareTo(CovidSortBean o) {
        int result = this.getCases() - o.getCases();
        return result * -1;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(state);
        dataOutput.writeInt(cases);
        dataOutput.writeInt(deaths);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.state = dataInput.readUTF();
        this.cases = dataInput.readInt();
        this.deaths = dataInput.readInt();
    }
}
